package mx.vlabs.soapdroid.wsdl2java.ui;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;

import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JTextField;

import org.ow2.easywsdl.wsdl.api.WSDLException;

import mx.vlabs.soapdroid.wsdl2java.ui.exceptions.WizardPanelDataValidationException;

import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class WSDLSourcePanel extends WizardPanel {
	
	private JComboBox cbWsdlSource;
	
	private static String SANDBOX_WSDL = "http://localhost:8330/ws-sandbox/sandBoxService/SandBoxEjbBean?wsdl";
	private static String CURRENCY_COVERTOR_WSDL = "http://www.webservicex.net/currencyconvertor.asmx?WSDL";
	
	/**
	 * Create the panel.
	 */
	public WSDLSourcePanel() {
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new LineBorder(new Color(128, 128, 128), 1, true), "WSDL Url", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(259, Short.MAX_VALUE))
		);
			panel.setLayout(new GridLayout(0, 1, 0, 0));
		
			
		cbWsdlSource = new JComboBox();
		cbWsdlSource.setEditable(true);
		cbWsdlSource.addItem(CURRENCY_COVERTOR_WSDL);
		cbWsdlSource.addItem(SANDBOX_WSDL);
		panel.add(cbWsdlSource);
			
		
		cbWsdlSource.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub
				//cbWsdlSource.get
			}
		});
			
		
		
		setLayout(groupLayout);

	}
	
	
	@Override
	public void onNext(NavigationPanel nav) throws WizardPanelDataValidationException{
		// TODO Auto-generated method stub
		
		String wsdl = cbWsdlSource.getSelectedItem().toString();
		
		cbWsdlSource.setEnabled(false);
		
		
		try {
			getParentFrame().getWsdl2Java().setPackageName(null);
			getParentFrame().getWsdl2Java().readAndInit(wsdl);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new WizardPanelDataValidationException(e);
		}
	
		super.onNext(nav);
		cbWsdlSource.setEnabled(true);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	
}
